<template>
  <view class="container">
    <view class="header">
      <view class="back-btn" @click="goBack">
        <image src="/path/to/back-icon.png" mode="aspectFit"></image>
      </view>
      <view class="title">{{title}}</view>
      <view class="save-btn" @click="saveAddress">保存</view>
    </view>
    <view class="form-item">
      <view class="label">所在城市:</view>
      <input type="text" placeholder="如杭州市" v-model="address.city" />
    </view>
    <view class="form-item">
      <view class="label">小区/大厦/学校:</view>
      <input type="text" placeholder="如杭师大仓前校区" v-model="address.department" />
    </view>
    <view class="form-item">
      <view class="label">楼号-门牌号:</view>
      <input type="text" placeholder="A号楼B层" v-model="address.houseNumber" />
    </view>
    <view class="form-item">
      <view class="label">收货人:</view>
      <input type="text" placeholder="请填写收货人的姓名" v-model="address.name" />
    </view>
    <view class="form-item">
      <view class="label">联系电话:</view>
      <input type="number" placeholder="请填写收货手机号" v-model="address.phone" />
    </view>
  </view>
</template>

<script>
	import  config  from '@/config'
export default {
  data() {
    return {
      title: '新建收货地址',
      logo:false,
      address: {
        city: '',
        department: '',
        houseNumber: '',
        name: '',
        phone: '',
        _id: '',
        createdAt: '',
        updatedAt: '',
      }
    };
  },
  onLoad(options) {
    if (options.id) {
      const addressId = options.id;
      uni.request({
        url: config.API_HOST+`/adress/${addressId}`, // 修正URL拼写错误
        success: (res) => {
          if (res.statusCode === 200) {
            this.title = '编辑收货地址';
            this.address = res.data;
            this.logo=true
          } else {
            uni.showToast({
              title: '获取地址信息失败',
              icon: 'none'
            });
            console.error('获取地址信息失败:', res.data);
          }
        }
      });
    }
  },
  methods: {
    saveAddress() {
      if (!this.address.city ||!this.address.department ||!this.address.houseNumber ||!this.address.name ||!this.address.phone) {
        uni.showToast({
          title: '请填写完整信息',
          icon: 'none'
        });
        return;
      }
      const now = new Date().toISOString();
      this.address.createdAt = now;
      this.address.updatedAt = now;
      uni.request({
        url: this.logo? config.API_HOST+`/adress/${this.address.id}` : config.API_HOST+'/adress',
        method: this.logo? 'PUT' : 'POST',
        data: this.address,
        success: (res) => {
          if (res) {
            uni.showToast({
              title: '保存成功',
              icon:'success'
            });
            uni.navigateBack();
          } else {
            uni.showToast({
              title: '保存失败',
              icon: 'none'
            });
            console.error('保存地址信息失败:', res.data);
          }
        }
      });
    },
    goBack() {
      uni.navigateBack();
    }
  }
};
</script>

<style>
.container {
  padding: 10px;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.back-btn {
  width: 30px;
  height: 30px;
}
.title {
  font-size: 20px;
  font-weight: bold;
}
.save-btn {
  font-size: 16px;
  color: #007AFF;
}
.form-item {
  margin-bottom: 10px;
  display: flex;
  align-items: center;
}
.label {
  width: 100px;
  font-size: 16px;
}
input {
  flex: 1;
  height: 30px;
  border: 1px solid #E5E5E5;
  border-radius: 3px;
  padding: 0 5px;
}
</style>